package com.xls.box2023.mvp;

import android.app.DownloadManager;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.lzz.bottomencapsulation.consts.EventbusCode;
import com.lzz.bottomencapsulation.consts.NetConsts;
import com.lzz.bottomencapsulation.entity.VersionEntity;
import com.lzz.bottomencapsulation.eventbus.MessageEvent;
import com.lzz.bottomencapsulation.manager.AppManager;
import com.lzz.bottomencapsulation.manager.DownloadApkManage;
import com.lzz.bottomencapsulation.mvp.LBMVPBaseActivity;
import com.lzz.bottomencapsulation.utils.LUtils;
import com.lzz.bottomencapsulation.utils.SPUtils;
import com.xls.box2023.login.LoginActivity;
import com.xls.box2023.login.LoginUtil;
import com.xls.resource.dialog.APKDownloadDialog;
import com.xls.resource.dialog.APKInsDialog;
import com.xls.resource.dialog.EditionUpDialog;
import com.xls.resource.dialog.VersionDialog;

public abstract class MVPBaseActivity<V extends BaseView,T extends BasePresenterImpl<V>> extends LBMVPBaseActivity<V,T> implements BaseView{
    VersionDialog versionDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onEvent(MessageEvent messageEvent) {
        super.onEvent(messageEvent);
        if (NetConsts.CODE_50001==messageEvent.getCode()||NetConsts.CODE_50002==messageEvent.getCode()){
            upVersion((VersionEntity) messageEvent.getData());// APK更新
        }else  if (messageEvent.getCode()==EventbusCode.CODE_OUTLOGIN){//退出登录
            if (!AppManager.getAppManager().currentActivity().getClass().equals(LoginActivity.class)){
                LoginUtil.getInstance().outLogin(this);
            }
        }
    }

    /**
     * 更新版本
     * @param data
     */
    private void upVersion(VersionEntity data) {
        if (data==null||TextUtils.isEmpty(data.getUrl())) return;
        VersionEntity versionEntity=SPUtils.getdownloadAPK();
        if (versionEntity !=null&&data.getVersion().equals(versionEntity.getVersion())){
            versionEntity.setForceUpdate(data.isForceUpdate());
            int state=  DownloadApkManage.getInstance().ifDownload(this,versionEntity.getDwonloadID());
            LUtils.e("下载状态"+state);
            if (state==DownloadManager.STATUS_SUCCESSFUL){
                showEditionInsDialog(versionEntity);//安装
            }else if (state==DownloadManager.STATUS_RUNNING){
                //              正在下载中
                showApkDownladPW(versionEntity);
            }else {
//              下载失败重新下载
                showEditionUpDialog(versionEntity);
            }
        }else {
            showEditionUpDialog(data);
        }
    }

    APKInsDialog apkInsDialog;
    private void showEditionInsDialog(VersionEntity data) {
        if (AppManager.getAppManager().currentActivity()!=this) return;//只在当前activity显示
        if (data==null) return;
        if (apkDownloadDialog!=null){
            if (apkDownloadDialog.isShowing())apkDownloadDialog.dismiss();
        }
        if (editionUpDialog!=null){
            if (editionUpDialog.isShowing())editionUpDialog.dismiss();
        }
        if (apkInsDialog==null){
            apkInsDialog=new APKInsDialog(this);
        }
        if (apkInsDialog.isShowing()) return;
        apkInsDialog.setVersionEntity(data);
        apkInsDialog.show();
    }

    /**
     * 显示版本升级弹窗
     * @param editionEntity
     */
    EditionUpDialog editionUpDialog=null;
    private void showEditionUpDialog(VersionEntity editionEntity) {
        if (AppManager.getAppManager().currentActivity()!=this) return;//只在当前activity显示
        if (editionEntity==null) return;
        if (apkDownloadDialog!=null){
            if (apkDownloadDialog.isShowing())apkDownloadDialog.dismiss();
        }
        if (apkInsDialog!=null){
            if (apkInsDialog.isShowing())apkInsDialog.dismiss();
        }
        if (editionUpDialog==null){
            editionUpDialog=new EditionUpDialog(getActivity());
        }
        if (!editionUpDialog.isShowing()){
            editionUpDialog.setVersionEntity(editionEntity);
            editionUpDialog.show();
        }
    }

    /**
     * apk正在下载弹窗
     * @param activity
     */
    APKDownloadDialog apkDownloadDialog;
    private void showApkDownladPW(VersionEntity versionEntity) {
        if (AppManager.getAppManager().currentActivity()!=this) return;//只在当前activity显示
        if (versionEntity==null) return;
        if (editionUpDialog!=null){
            if (editionUpDialog.isShowing())editionUpDialog.dismiss();
        }
        if (apkInsDialog!=null){
            if (apkInsDialog.isShowing())apkInsDialog.dismiss();
        }
        if (apkDownloadDialog==null){
            apkDownloadDialog=new APKDownloadDialog(this);
        }
        if (apkDownloadDialog.isShowing()) return;
        apkDownloadDialog.setVersionEntity(versionEntity);
        apkDownloadDialog.show();
    }
}

